﻿using DevExpress.XtraBars;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;

namespace Git
{
    partial class git
    {
        private bool gitclonetexst = false;

        void p_gitclone_OutputDataReceived(object sender, DataReceivedEventArgs e)
        {
            if (e.Data != null)
            {
                if (Regex.IsMatch(e.Data, "git clone"))
                {
                    gitclonetexst = true;
                }
                Console.WriteLine(e.Data);
            }
        }

        //项目路径
        private string gitProduct;

        private string getCloneDirectory(string url)
        {
            string thisExeDirectory = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
            string thisDirectory = thisExeDirectory + @"JSONGitDataList";
            if (!Directory.Exists(thisDirectory))
            {
                Directory.CreateDirectory(thisDirectory);
            }
            string locurl = EncryptWithMD5(url);
            gitProduct = thisDirectory + "\\" + locurl;
            git.gitexelist.Add(new gitconfig
            {
                Key = gitProduct,
                Value = "GITProductURL",
                Branch = locurl,
                ID = System.Guid.NewGuid().ToString(),
                ParentID = ""
            });
            return gitProduct;
        }

        public static string EncryptWithMD5(string source)
        {
            byte[] sor = Encoding.UTF8.GetBytes(source);
            MD5 md5 = MD5.Create();
            byte[] result = md5.ComputeHash(sor);
            StringBuilder strbul = new StringBuilder(40);
            for (int i = 0; i < result.Length; i++)
            {
                strbul.Append(result[i].ToString("x2"));//加密结果"x2"结果为32位,"x3"结果为48位,"x4"结果为64位

            }
            return strbul.ToString();
        }

        public Boolean gitclone(string file,string url)
        {
            try
            {
                Process p = new Process();
                p.StartInfo.FileName = "cmd.exe";
                p.StartInfo.UseShellExecute = false;    //是否使用操作系统shell启动
                p.StartInfo.RedirectStandardInput = true;//接受来自调用程序的输入信息
                p.StartInfo.RedirectStandardOutput = true;//由调用程序获取输出信息
                p.StartInfo.RedirectStandardError = true;//重定向标准错误输出
                p.StartInfo.CreateNoWindow = true;//不显示程序窗口
                p.OutputDataReceived += p_gitclone_OutputDataReceived;
                p.Start();
                p.StandardInput.WriteLine(@"cd """ + file + @"""");
                p.StandardInput.WriteLine("git clone " + url + " " + getCloneDirectory(url));
                p.StandardInput.WriteLine("exit");
                p.BeginOutputReadLine();
                p.WaitForExit();
                p.Close();
                if (!Directory.Exists(gitProduct))
                {
                    return false;
                }
                return gitclonetexst;
            }
            catch (Exception e) {
                return false;
            }
        }

    }
}
